/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.synjones.pay.serv.recharge.helper;

import com.synjones.pay.model.common.CommonConstant;
import com.synjones.pay.model.common.ResponseBean;
import com.synjones.pay.model.context.ContextFactory;
import com.synjones.pay.model.context.IPayContext;
import com.synjones.pay.model.context.OrderIdGenerator;
import com.synjones.pay.model.context.StatusInfo;
import com.synjones.pay.model.recharge.RechargeReqBean;
import com.synjones.pay.model.recharge.RechargeRespBean;
import com.synjones.pay.model.recharge.RechargeTradeBean;
import com.synjones.pay.serv.dao.bean.RechargeDao;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;
import org.springframework.beans.BeanUtils;

/**
 *
 * @author hsd
 */
public class RechargeHelper {

    private static final String RECHARGE_SYS_CODE = "RG";
    private static final String RECHARGE_FAIL_SAVE_DESC = "系统忙，请重新提交。";

    public static String generateOrderId() {
        return OrderIdGenerator.nextId(RECHARGE_SYS_CODE);
    }

    public static String generateCurrTime() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("uuuuMMddHHmmss"));
    }

    public static RechargeDao assemDaoData(RechargeReqBean reqBean) {
        //1.产生订单
        String uuid = UUID.randomUUID().toString();
        String orderId = generateOrderId();
        String insertTime = generateCurrTime();
        //2.转换对象数据到dao
        RechargeDao dao = new RechargeDao();
        BeanUtils.copyProperties(reqBean, dao);
        dao.setOrderId(orderId);
        dao.setInsertTime(insertTime);
        dao.setUuid(uuid);
        return dao;
    }

    public static RechargeRespBean failRespData(RechargeReqBean reqBean) {
        RechargeRespBean bean = new RechargeRespBean();
        BeanUtils.copyProperties(reqBean, bean);
        bean.setStatusCode(StatusInfo._9000.code());
        bean.setStatusDesc(RECHARGE_FAIL_SAVE_DESC);
        return bean;
    }

    public static IPayContext assemTradeData(RechargeDao dao) {
        IPayContext ctx = ContextFactory.getDefaultContext();
        RechargeTradeBean tradeBean = new RechargeTradeBean();
        BeanUtils.copyProperties(dao, tradeBean);
        ctx.getRequest().add(CommonConstant.TRADE_OBJ, tradeBean);
        return ctx;
    }

    public static RechargeRespBean assemFrontRespBean(ResponseBean respBean) {
        RechargeRespBean bean = new RechargeRespBean();
        BeanUtils.copyProperties(respBean, bean);
        bean.setPaytoolCode(respBean.getBankCode());
        bean.setOrderId(respBean.getOrderNo());
        return bean;
    }

}
